package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.entity.fr.indicators.MonthIndicators;
import com.plian.system.entity.yth.finance.MonthlyDebt;
import com.plian.system.mapper.yth.finance.MonthlyDebtMapper;
import com.plian.system.service.yth.IMonthlyDebtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 月度指标录入
 * @Author: jatpen
 * @Date: 2023/5/9 2:33 PM
 */
@Slf4j
@Service
public class MonthlyDebtServiceImpl extends ServiceImpl<MonthlyDebtMapper, MonthlyDebt> implements IMonthlyDebtService {

    private final String YTH_DATASOURCE_TYPE = "3";


    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<MonthIndicators> getSyncData(Date startTime, Date endTime) {
        List<MonthIndicators> monthIndicatorsList = new ArrayList<>();
        List<MonthlyDebt> monthlyDebts = list(new LambdaQueryWrapper<MonthlyDebt>().gt(Optional.ofNullable(startTime).isPresent(), MonthlyDebt::getCreateTime, startTime).le(Optional.ofNullable(endTime).isPresent(), MonthlyDebt::getCreateTime, endTime));
        if (CollectionUtil.isNotEmpty(monthlyDebts)) {
            monthIndicatorsList = conversion(monthlyDebts);
        }
        return monthIndicatorsList;
    }

    /**
     * 转换对象
     *
     * @param monthlyDebts
     * @return
     */
    private List<MonthIndicators> conversion(List<MonthlyDebt> monthlyDebts) {

        List<MonthIndicators> monthIndicatorsList = new ArrayList<>();

        for (MonthlyDebt monthlyDebt : monthlyDebts) {
            MonthIndicators monthIndicators = new MonthIndicators();
            monthIndicators.setOrgId(Objects.toString(monthlyDebt.getReportOrgId(), null));
            monthIndicators.setGroupId(Objects.toString(monthlyDebt.getBelongGroupId(), null));
            monthIndicators.setAssigning(Objects.toString(monthlyDebt.getCompanyLevel(), null));
            monthIndicators.setFyear(Objects.toString(monthlyDebt.getYear(), null));
            monthIndicators.setGroupName(monthlyDebt.getBelongGroupName());
            monthIndicators.setFmonth(Objects.toString(monthlyDebt.getMonth()));
            monthIndicators.setBookAssetLiability(monthlyDebt.getCurrentBookDebtRatio() == null ? null : monthlyDebt.getCurrentBookDebtRatio().toString());
            monthIndicators.setActualAssetLiability(monthlyDebt.getCurrentActualDebtRatio() == null ? null : monthlyDebt.getCurrentActualDebtRatio().toString());
            monthIndicators.setMultipleInterest(monthlyDebt.getCurrentInterestMultiple() == null ? null : monthlyDebt.getCurrentInterestMultiple().toString());
            monthIndicators.setInterestBearing(monthlyDebt.getCurrentInterestDebtRatio() == null ? null : monthlyDebt.getCurrentInterestDebtRatio().toString());
            monthIndicators.setCashFlow(monthlyDebt.getCurrentCashFlowRatio() == null ? null : monthlyDebt.getCurrentCashFlowRatio().toString());
            monthIndicators.setGroupBookAssetLiability(monthlyDebt.getCollectBookDebtRatio() == null ? null : monthlyDebt.getCollectBookDebtRatio().toString());
            monthIndicators.setGroupActualAssetLiability(monthlyDebt.getCollectActualDebtRatio() == null ? null : monthlyDebt.getCollectActualDebtRatio().toString());
            monthIndicators.setGroupMultipleInterest(monthlyDebt.getCollectInterestMultiple() == null ? null : monthlyDebt.getCollectInterestMultiple().toString());
            monthIndicators.setGroupInterestBearing(monthlyDebt.getCollectCashFlowRatio() == null ? null : monthlyDebt.getCollectCashFlowRatio().toString());
            monthIndicators.setGroupCashFlow(monthlyDebt.getCollectBookDebtRatio() == null ? null : monthlyDebt.getCollectBookDebtRatio().toString());
            monthIndicators.setTotalBookAssets(monthlyDebt.getCurrentTotalFunds() == null ? null : monthlyDebt.getCurrentTotalFunds().toString());
            monthIndicators.setCreditFalseAssets(monthlyDebt.getCurrentAssetsCredit() == null ? null : monthlyDebt.getCurrentAssetsCredit().toString());
            monthIndicators.setAdjustedTotalAssets(monthlyDebt.getCurrentAdjustTotalFunds() == null ? null : monthlyDebt.getCurrentAdjustTotalFunds().toString());
            monthIndicators.setTotalBookLiabilities(monthlyDebt.getCurrentTotalDebt() == null ? null : monthlyDebt.getCurrentTotalDebt().toString());
            monthIndicators.setMingRealDebt(monthlyDebt.getCurrentPaymentEquityTool() == null ? null : monthlyDebt.getCurrentPaymentEquityTool().toString());
            monthIndicators.setAdjustedTotalLiabilities(monthlyDebt.getCurrentAdjustTotalDebt() == null ? null : monthlyDebt.getCurrentAdjustTotalDebt().toString());
            monthIndicators.setGroupTotalBookAssets(monthlyDebt.getCollectTotalFunds() == null ? null : monthlyDebt.getCollectTotalFunds().toString());
            monthIndicators.setGroupCreditFalseAssets(monthlyDebt.getCollectAssetsCredit() == null ? null : monthlyDebt.getCollectAssetsCredit().toString());
            monthIndicators.setGroupAdjustedTotalAssets(monthlyDebt.getCollectAdjustTotalFunds() == null ? null : monthlyDebt.getCollectAdjustTotalFunds().toString());
            monthIndicators.setGroupTotalBookLiabilities(monthlyDebt.getCollectTotalDebt() == null ? null : monthlyDebt.getCollectTotalDebt().toString());
            monthIndicators.setGroupMingRealDebt(monthlyDebt.getCollectPaymentEquityTool() == null ? null : monthlyDebt.getCollectPaymentEquityTool().toString());
            monthIndicators.setGroupAdjustedTotalLiabilities(monthlyDebt.getCollectAdjustTotalDebt() == null ? null : monthlyDebt.getCollectAdjustTotalDebt().toString());
            monthIndicators.setFormType(String.valueOf(FormTypeConstant.FR_MONTHINDICATORS));
            monthIndicators.setDataSource(YTH_DATASOURCE_TYPE);
            monthIndicators.setDataSourceId(monthlyDebt.getId().toString());
            monthIndicators.setStatus("0");
            monthIndicators.setCode(monthlyDebt.getNumber());
            monthIndicators.setCreateUser(Objects.toString(monthlyDebt.getReportUserId(), null));
            monthIndicators.setCreateTime(new Date());
            monthIndicatorsList.add(monthIndicators);
        }
        return monthIndicatorsList;
    }
}
